package jsyf.oa.service.device;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.CompanyBrandDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.entity.system.Company2BrandEntity;
import jsyf.oa.facade.device.CompanyBrandFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by y on 2018/3/12.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class CompanyBrandService implements CompanyBrandFacade {
    @Autowired
    private CompanyBrandDao companyBrandDao;
    @Autowired
    private UserDao userDao;

    @Override
    public JSONObject getCompanyBrandList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //公司ID
        String companyId = jsonParams.getString("companyId");
        //品牌ID
        String brandId = jsonParams.getString("brandId");
        //公司名称
        String companyName = jsonParams.getString("companyName");
        //品牌名称
        String brandName = jsonParams.getString("brandName");
        //操作时间
        String createTime = jsonParams.getString("createTime");
        //操作人员
        String createUser = jsonParams.getString("createUser");
        //备注
        String remark = jsonParams.getString("remark");
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;

        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();

        if (!StringUtils.isBlank(companyName)) {
            condition.append(" AND companyName=?");
            sqlArgs.add(companyName);
        }
        if (!StringUtils.isBlank(brandName)) {
            condition.append(" AND brandName=?");
            sqlArgs.add(brandName);
        }
        if (!StringUtils.isBlank(remark)) {
            condition.append(" AND remark=?");
            sqlArgs.add(remark);
        }
        if (!StringUtils.isBlank(createTime)) {
            condition.append(" AND createTime=?");
            sqlArgs.add(createTime);
        }
        if (!StringUtils.isBlank(createUser)) {
            condition.append(" AND createUser like ?");
            sqlArgs.add("%" + createUser + "%");
        }
        condition.append("AND delFlag=0");
        condition.append(" ORDER BY createTime DESC");
        //查询list
        List<JSONObject> records = companyBrandDao.getViewJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = companyBrandDao.getViewPageCount(condition.toString(), sqlArgs.toArray());

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;

    }

    @Override
    public JSONObject findCompanyList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId=jsonParams.getString("userId");
        JSONObject userJson = userDao.getViewJSONListByCondition(" AND id = ? AND main = 1 ",new Object[]{userId}).get(0);
        String companyId = userJson.get("companyId").toString();
        String companyName = userJson.get("companyName").toString();
        JSONObject obj = new JSONObject();
        StringBuffer condition = new StringBuffer();
        List<JSONObject> companies=new ArrayList<JSONObject>();
        if("201712161613432EAF27DBCCB7D0E6CA".equals(companyId)){
            condition.append("SELECT");
            condition.append(" t_sys_company.ID as id,");
            condition.append(" IFNULL(t_sys_company.All_NAME,t_sys_company.`NAME`) as name,");
            condition.append(" t_sys_company.NAME  as companyName");
            condition.append(" FROM `t_sys_company_2_brand` LEFT JOIN t_sys_company ON t_sys_company_2_brand.COMPANY_ID = t_sys_company.ID");
            condition.append(" WHERE t_sys_company.DEL_FLAG=0 and t_sys_company.PARENT_ID = '201712161613432EAF27DBCCB7D0E6CA'AND t_sys_company.NAME NOT LIKE '%大客户%'\n" +
                    "AND t_sys_company.NAME NOT LIKE '%瀛沣%' GROUP BY t_sys_company.ID ORDER BY t_sys_company.SORT_NO");
             companies = companyBrandDao.queryForJsonList(condition.toString(), new Object[]{});
            obj.put("companies", companies);
        }else{
            JSONObject company = new JSONObject();
            company.put("id",companyId);
            company.put("name",companyName);
            companies.add(company);
            obj.put("companies", companies);
        }
        return obj;
    }

    @Override
    public JSONObject findBrandListByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject obj = new JSONObject();
        StringBuffer condition = new StringBuffer();
        condition.append("SELECT");
        condition.append(" T_DVC_BRAND.ID AS id,");
        condition.append(" T_DVC_BRAND.BRAND_NAME AS name");
        condition.append(" FROM T_DVC_BRAND ");
        condition.append(" LEFT JOIN T_SYS_COMPANY_2_BRAND ON");
        condition.append(" T_DVC_BRAND.ID=T_SYS_COMPANY_2_BRAND.BRAND_ID");
        condition.append(" WHERE ");
        condition.append(" T_SYS_COMPANY_2_BRAND.COMPANY_ID=? ");
        String id = jsonParams.getString("companyId");
        List<JSONObject> brands = companyBrandDao.queryForJsonList(condition.toString(), new Object[]{id});
        obj.put("brands", brands);
        return obj;
    }

    @Override
    public String add(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        jsonParams.put("delFlag", "0");
        Company2BrandEntity company2BrandEntity = JSON.toJavaObject(jsonParams, Company2BrandEntity.class);
        int num = companyBrandDao.add(company2BrandEntity);
        if (num > 0) {
            return BaseUtils.succsessObjRespond("添加成功");
        } else {
            return BaseUtils.errorrRespond("添加失败");
        }
    }

    @Override
    public Boolean update(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Company2BrandEntity company2BrandEntity = JSON.toJavaObject(jsonParams, Company2BrandEntity.class);
        int num = companyBrandDao.update(company2BrandEntity);
        return num > 0;
    }

    @Override
    public Boolean remove(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId=jsonParams.getString("companyId");
        String brandId=jsonParams.getString("brandId");
        StringBuffer condition=new StringBuffer();
        condition.append(" AND COMPANY_ID=? AND BRAND_ID=?");
        int num = companyBrandDao.removeByCondition(condition.toString(),new String[]{companyId,brandId});
        return num > 0;
    }


}
